package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.webcontextdialog.WebContextDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnWebContextNode extends Shape {

	private static final long serialVersionUID = -6355397359525079488L;

	public static final String WEB_CONTEXT = "webContext";
	public static final String SHOW_WEB_CONTEXT = Messages
			.getString("SaturnWebContextNode.WebPageContext"); //$NON-NLS-1$

	public static final String WEB_CONTEXT_NAME = "name";

	public static final String WEB_CONTEXT_DESCRIPTION = "description";

	public static final String WEB_CONTEXT_VALUE_MOVES = "value-moves";

	public static final String WEB_CONTEXT_VALUE_MOVE = "value-move";

	public static final String WEB_CONTEXT_ID = "id";

	public static final String WEB_CONTEXT_KEY = "key";

	public static final String WEB_CONTEXT_SCOPE = "scope";

	public static final String WEB_CONTEXT_NEW_KEY = "newKey";

	public static final String WEB_CONTEXT_NEW_SCOPE = "newScope";

	public static final String WEB_CONTEXT_ISDELETE = "isDelete";

	private Image image = Activator.getImage(Images.WEB_CONTEXT_IMAGE);

	public static int number = 1;

	private List<SaturnWebContextParameter> saturnWebContextParameterList = new ArrayList<SaturnWebContextParameter>();

	public SaturnWebContextNode() {
		super.setName(SHOW_WEB_CONTEXT + number++);
		super.setSize(new Dimension(50, 50));
	}

	public Image getIcon() {
		return image;
	}

	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	@Override
	public Dialog getDialog() {
		WebContextDialog webContextDialog = new WebContextDialog(super
				.getShell(), this);
		super.reorder(this);
		return webContextDialog;
	}

	public Node doRefresh() {
		Node root = super.getRoot();

		Node webContextNode = SaturnW3CUtil.createChildNode(root, WEB_CONTEXT,
				null);

		SaturnW3CUtil.createChildNode(webContextNode, WEB_CONTEXT_NAME, super
				.getName());

		createWebParameters(webContextNode, getSaturnWebContextParameterList());

		return webContextNode;
	}

	private void createWebParameters(Node node,
			List<SaturnWebContextParameter> webParameters) {
		Node webParametersNode = SaturnW3CUtil.createChildNode(node,
				WEB_CONTEXT_VALUE_MOVES, null);
		if (webParameters != null) {
			for (SaturnWebContextParameter parameter : webParameters) {
				this.createParameterNode(webParametersNode, parameter);
			}
		}
	}

	private void createParameterNode(Node webParametersNode,
			SaturnWebContextParameter webParameter) {
		Node node = SaturnW3CUtil.createChildNode(webParametersNode,
				WEB_CONTEXT_VALUE_MOVE, null);

		SaturnW3CUtil
				.createAttribute(node, WEB_CONTEXT_ID, webParameter.getId());
		SaturnW3CUtil.createAttribute(node, WEB_CONTEXT_KEY, webParameter
				.getKey());
		SaturnW3CUtil.createAttribute(node, WEB_CONTEXT_SCOPE, webParameter
				.getScope());
		SaturnW3CUtil.createAttribute(node, WEB_CONTEXT_NEW_KEY, webParameter
				.getNewKey());
		SaturnW3CUtil.createAttribute(node, WEB_CONTEXT_NEW_SCOPE, webParameter
				.getNewScope());
		SaturnW3CUtil.createAttribute(node, WEB_CONTEXT_ISDELETE, webParameter
				.getIsDelete());
	}

	public List<SaturnWebContextParameter> getSaturnWebContextParameterList() {
		return saturnWebContextParameterList;
	}

	public void setSaturnWebContextParameterList(
			List<SaturnWebContextParameter> saturnWebContextParameterList) {
		if (this.saturnWebContextParameterList.size() != saturnWebContextParameterList
				.size()) {
			this.saturnWebContextParameterList = saturnWebContextParameterList;
			firePropertyChange(PROPERTERY_CHANGS, null,
					saturnWebContextParameterList);
			return;
		}
		for (SaturnWebContextParameter contextParameter : this.saturnWebContextParameterList) {
			for (SaturnWebContextParameter newContextParameter : saturnWebContextParameterList) {
				if (!contextParameter.equals(newContextParameter)) {
					this.saturnWebContextParameterList = saturnWebContextParameterList;
					firePropertyChange(PROPERTERY_CHANGS, null,
							saturnWebContextParameterList);
				}
			}
		}
	}

	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnWebContextNode contextNode = new SaturnWebContextNode();
		SaturnWebContextNode saturn = (SaturnWebContextNode) shape;
		contextNode.setLocation(mousePoint);
		contextNode.setName(saturn.getName());
		contextNode.setDescription(saturn.getDescription());
		contextNode.setSaturnWebContextParameterList(saturn
				.getSaturnWebContextParameterList());
		contextNode.setId(saturn.getId());
		contextNode.setNextId(saturn.getNextId());
		return contextNode;
	}

	protected void setImage(Image image) {
		this.image = image;
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		String name = getName();
		Parameters para = new Parameters();

		para.setName(getName());
		para.setId(getId());
		para.setDescription(getDescription());
	 	basicvaluse.add(para);
		
		
		para.setInfourl( name + "-web_context.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Action.WebContextNode")); //$NON-NLS-1$
		para.setImagename("web_context");
		
		summm.put("parameters", createParaList());
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/action/webcontext";
		String Path = filePath + File.separator + name + "-web_context.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,summm, Path);
		
		return para;
	}
	
	/**
	 * 创建网页上下文节点中的参数列表
	 * 
	 * @param webcontextnode
	 * @return  List<Parameters>
	 */
	private List<Parameters> createParaList() {
		
		List<SaturnWebContextParameter> parameters = getSaturnWebContextParameterList();
		List<Parameters> paralist = new ArrayList<Parameters>();
		
		for (SaturnWebContextParameter saturnWebContextParameter : parameters) {
			Parameters para = new Parameters();

			para.setId(saturnWebContextParameter.getId());
			para.setKey(saturnWebContextParameter.getKey());
			para.setScope(transferScope(saturnWebContextParameter.getScope()));
			para.setNewkey(saturnWebContextParameter.getNewKey());
			para.setNewscope(transferScope(saturnWebContextParameter.getNewScope()));
			
			paralist.add(para);
		}
		
		return paralist;
	}
	
	/**
	 * 网页上下文节点中上下文范围的转换
	 * 
	 * @param key
	 * @return  String
	 */
	private static String transferScope(String key) {
		if(key.equals("1")) {
			return "logic";
		} else if(key.equals("2")) {
			return "request";
		} else if(key.equals("5")) {
			return "session";
		} else {
			return "application";
		}
	}
}
